//
//  TJOrderListCell.m
//  Teleport
//
//  Created by hc-101 on 2020/6/12.
//  Copyright © 2020 xtjun. All rights reserved.
//

#import "TJAdListCell.h"
#import "TJOTCPayWayCell.h"


@interface TJAdListCell()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (weak, nonatomic) UILabel *numberLabel;
@property (strong, nonatomic) NSDateFormatter *formatter;
@property (strong, nonatomic) UIImageView *iconImageView;
@property (strong, nonatomic) UILabel *limitLabel;
@property (strong, nonatomic) UILabel *nameLabel;
@property (strong, nonatomic) UILabel *priceValueLabel;
@property (strong, nonatomic) UILabel *buyLabel;
@property (strong, nonatomic) UICollectionViewFlowLayout *flowLayout;
@property (weak, nonatomic) UICollectionView *collectionView;
@property (strong, nonatomic) NSMutableArray *payWayIconDatas;



@end

static NSString * const payWayCell = @"payWayCell";


@implementation TJAdListCell

- (void)settingSubView {
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    UIView *tj_contentView = [UIView tj_addToView:self.contentView];
    tj_contentView.layer.shadowOffset = CGSizeMake(0, 3);
    tj_contentView.backgroundColor = UIColor.whiteColor;
    tj_contentView.layer.shadowColor = [UIColor.blackColor colorWithAlphaComponent:0.4].CGColor;
    tj_contentView.layer.shadowRadius = 5;
    tj_contentView.layer.shadowOpacity = 0.3;
    tj_contentView.layer.cornerRadius = 5;
    tj_contentView.layer.masksToBounds = NO;
    [tj_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(5);
        make.left.offset(16);
        make.right.offset(-16);
        make.bottom.offset(-5);
    }];
    
    self.contentView.backgroundColor = UIColor.whiteColor;
    
    _iconImageView = [UIImageView tj_addToView:tj_contentView];
    _iconImageView.backgroundColor = UIColor_Hex(0xD8D8D8);
    [_iconImageView shearRoundedCornersWithRadiu:10];
    [_iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.offset(12);
        make.width.offset(20);
        make.height.offset(20);
    }];
    
    
    _nameLabel = [UILabel lableWithSuperView:tj_contentView font:UIFont_Px(15) color:UIColor_Hex(0x262626) title:@"" textAlignment:NSTextAlignmentLeft];
    [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_iconImageView.mas_right).offset(16);
        make.centerY.equalTo(_iconImageView);
    }];
    
    _numberLabel = [UILabel lableWithSuperView:tj_contentView font:UIFont_Px(12) color:UIColor_Hex(0x9E9E9E) title:[NSString stringWithFormat:@"%@", @""] textAlignment:NSTextAlignmentLeft];
    [_numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(10);
        make.top.equalTo(_iconImageView.mas_bottom).offset(10);
    }];
    
    _limitLabel = [UILabel lableWithSuperView:tj_contentView font:UIFont_Px(12) color:UIColor_Hex(0x9E9E9E) title:@"" textAlignment:NSTextAlignmentLeft];
    [_limitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_numberLabel);
        make.top.equalTo(_numberLabel.mas_bottom).offset(8);
    }];
    
    UILabel *priceLabel = [UILabel lableWithSuperView:tj_contentView font:UIFont_Px(13) color:UIColor_Hex(0x999999) title:@"单价".localizedString textAlignment:NSTextAlignmentLeft];
    [priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-10);
        make.centerY.equalTo(_numberLabel);
    }];
    
    _priceValueLabel = [UILabel lableWithSuperView:tj_contentView font:UIFont_Px(13) color:UIColor_Hex(0x262626) title:@"￥53" textAlignment:NSTextAlignmentLeft];
    [_priceValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-10);
        make.centerY.equalTo(_limitLabel);
    }];
    
    _buyLabel = [UILabel lableWithSuperView:tj_contentView font:UIFont_Px(15) color:UIColor.whiteColor title:@"购买".localizedString textAlignment:NSTextAlignmentCenter];
    _buyLabel.backgroundColor = UIColor_Hex(0x2B6CC7);
    [_buyLabel addTapGesturesWithTarget:self action:@selector(tapBuyLabel)];
    [_buyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_priceValueLabel);
        make.width.offset(90);
        make.height.offset(30);
        make.top.equalTo(_priceValueLabel.mas_bottom).offset(8);
    }];
    
    
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.flowLayout.minimumLineSpacing = 10;
    self.flowLayout.minimumInteritemSpacing = 10;
    self.flowLayout.itemSize = CGSizeMake(16, 16);
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.flowLayout];
    collectionView.backgroundColor = [UIColor whiteColor];
    collectionView.showsHorizontalScrollIndicator = NO;
    collectionView.dataSource = self;
    collectionView.delegate = self;
    collectionView.showsVerticalScrollIndicator = NO;
    collectionView.showsHorizontalScrollIndicator = NO;
    [collectionView registerClass:TJOTCPayWayCell.class forCellWithReuseIdentifier:payWayCell];
    [tj_contentView addSubview:collectionView];
    [collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_buyLabel);
        make.left.offset(10);
        make.width.offset(68);
        make.height.offset(20);
    }];
    
    _collectionView = collectionView;
    
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.payWayIconDatas.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    TJOTCPayWayCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:payWayCell forIndexPath:indexPath];
    cell.iconImageView.image = [UIImage imageNamed:self.payWayIconDatas[indexPath.row]];
    return cell;
}

- (void)setModel:(TJOrderListModel *)model {
    _model = model;
    
    _payWayIconDatas = @[].mutableCopy;
    
    NSArray*array = [model.payType componentsSeparatedByString:@"-"];
    
    for (NSString *payName in array) {
        if ([payName isEqualToString:@"WECHAT"]) {
            [_payWayIconDatas addObject:@"OTC_list_weChat"];
        }
        if ([payName isEqualToString:@"ALIAY"]) {
            [_payWayIconDatas addObject:@"OTC_list_Alipay"];
        }
        if ([payName isEqualToString:@"BANKCARD"]) {
            [_payWayIconDatas addObject:@"OTC_list_bank"];
        }
        
    }
    
    
    [_iconImageView sd_setImageWithURL:[NSURL URLWithString:model.headLink]];
    
    _nameLabel.text = model.memberFbName;
    _numberLabel.text = [NSString stringWithFormat:@"数量 %@%@".localizedString, model.num, model.currency];
    
    
    
    NSString *maxMoneyStr = [NSString stringWithFormat:@"%f",model.nowPrice.doubleValue * model.num.doubleValue];
    
    _limitLabel.text = [NSString stringWithFormat:@"%@ ¥%@~¥%@", @"限额".localizedString, model.minPrice,[Tools CNYString:maxMoneyStr]];
    _priceValueLabel.text =  [NSString stringWithFormat:@"¥%@", model.nowPrice];

        if ([model.status isEqualToString:@"NORMAL"]) {
               _buyLabel.text = @"下架".localizedString;
               _buyLabel.backgroundColor = UIColor.tj_mainColor;

           } else {
               _buyLabel.text = @"已下架".localizedString;
               _buyLabel.backgroundColor = UIColor.lightGrayColor;

           }
   
    [self.collectionView reloadData];
}
/**购买点击 */
- (void)tapBuyLabel {
    if ([self.delegate respondsToSelector:@selector(tapSellLabelWithCell:)]) {
        [self.delegate tapSellLabelWithCell:self];
    }
    
}

@end
